<?php

namespace app\merchant\controller;

use app\merchant\model\MerchantModel;
use think\admin\Controller;
use think\admin\model\SystemUser;


/**
 * 商户管理
 * @class Merchant
 * @package app\merchant\controller
 */
class Merchant extends Controller
{
    /**
     * 企业列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '企业列表';
        MerchantModel::mQuery()->where('is_deleted', 0)
            ->with('system_user')
            ->like('nickname,company,phone')
            ->equal('status,is_verification')
            ->dateBetween('login_at,create_at')
            ->order('id desc')
            ->layTable();
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {

    }

    /**
     * 企业详情
     * @auth true
     */
    public function detail()
    {
        $this->vo = MerchantModel::where('id', $this->request->get('id'))->find();
        $this->fetch();
    }

    /**
     * 添加企业
     * @auth true
     */
    public function add()
    {
//        $this->_applyFormToken();
        MerchantModel::mForm('form');
    }

    /**
     * 编辑企业
     * @auth true
     */
    public function edit()
    {
//        $this->_applyFormToken();
        MerchantModel::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $vo
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isGet()) {

        }else{

        }
    }

    protected function _form_result(bool $res, array $data){
        if (!$res) return true;
        $id = $data['id'];
        // 认证状态且没有绑定后台账号，创建一个后台账号
        $merchant = MerchantModel::where('id', $id)->find();
        if($merchant->is_verification == 1 && empty($merchant->system_user_id)){
            // 必须存在公司名称和联系电话
            if (empty($merchant->company) || empty($merchant->phone)) return true;
            $user = [];
            $user['usertype'] = 'merchant_admin';
            $user['contact_phone'] = $merchant->phone;
            // 账号是公司名称，密码是联系电话
            $user['username'] = $merchant->company;
            $user['nickname'] = $merchant->company;
            $user['password'] = md5($merchant->phone);
            // 固定企业用户的权限为id=2
            $user['authorize'] = arr2str([2]);
            // 检查账号是否重复
            $map = ['username' => $user['username'], 'is_deleted' => 0];
            $count = 0;
            while (SystemUser::mk()->where($map)->count() > 0) {
                $count ++;
                $map['username'] = $user['username'] . strval($count);
            }
            $user = SystemUser::create($user);
            // 更新绑定的账号
            $merchant->system_user_id = $user->id;
            $merchant->save();
        }
    }


    /**
     * 修改企业状态
     * @auth true
     */
    public function state()
    {
        MerchantModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除企业
     * @auth true
     */
    public function remove()
    {
        MerchantModel::mDelete();
    }
}
